package com.nfu.system.service.impl;

import com.nfu.system.domin.SysBill;
import com.nfu.system.domin.SysMovie;
import com.nfu.system.domin.dto.EvaluateDto;
import com.nfu.system.domin.vo.SysMovieVo;
import com.nfu.system.mapper.SysBillMapper;
import com.nfu.system.mapper.SysMovieMapper;
import com.nfu.system.service.SysMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SysMovieServiceImpl implements SysMovieService {

    @Autowired
    private SysMovieMapper sysMovieMapper;
    @Autowired
    private SysBillMapper sysBillMapper;

    @Override
    public List<SysMovie> findAllMovies(SysMovieVo sysMovieVo) {
        return sysMovieMapper.findAllMovies(sysMovieVo);
    }

    @Override
    public SysMovie findMovieById(Long id) {
        return sysMovieMapper.findMovieById(id);
    }

    @Override
    public SysMovie findOneMovie(Long id) {
        return sysMovieMapper.findOneMovie(id);
    }

    @Override
    public int addMovie(SysMovie sysMovie) {
        return sysMovieMapper.addMovie(sysMovie);
    }

    @Override
    public int updateMovie(SysMovie sysMovie) {
        return sysMovieMapper.updateMovie(sysMovie);
    }

    @Override
    public int deleteMovie(Long[] ids) {
        int rows = 0;
        for (Long id : ids) {
            rows += sysMovieMapper.deleteMovie(id);
        }
        return rows;
    }

//    @Override
//    public List<SysMovie> findByCinemaId(Long id) {
//        return sysMovieMapper.findByCinemaId(id);
//    }

    /**
     * 总票房榜
     *
     * @return
     */
    @Override
    public List<SysMovie> totalBoxOfficeList() {
        return sysMovieMapper.totalBoxOfficeList();
    }

    /**
     * 国内票房榜 已上映的国内电影里，按票房取前10 国内电影 movieArea in (港台+大陆)
     *
     * @return
     */
    @Override
    public List<SysMovie> domesticBoxOfficeList() {
        return sysMovieMapper.domesticBoxOfficeList();
    }

    /**
     * 国外票房榜 已上映的国外电影里，按票房取前10 国外电影 movieArea not in (港台+大陆)
     *
     * @return
     */
    @Override
    public List<SysMovie> foreignBoxOfficeList() {
        return sysMovieMapper.foreignBoxOfficeList();
    }

    /**
     * 热度榜 贝叶斯平均算法 + 基础分数
     *
     * @return
     */
    @Override
    public List<SysMovie> hotBoxOfficeList() {
        return sysMovieMapper.hotBoxOfficeList();
    }

    @Override
    public void evaluate(EvaluateDto dto) {
        SysBill sysBill = sysBillMapper.findBillById(dto.getBillId());
        if (sysBill == null) {
            throw new RuntimeException("订单不存在");
        }
        Long movieId = sysBill.getSysSession().getMovieId();
        SysMovie movie = sysMovieMapper.findOneMovie(movieId);
        int voteCount = movie.getVoteCount() + 1;
        double totalScore = movie.getTotalScore() + dto.getScore();
        SysMovie updateMovie = new SysMovie();
        updateMovie.setMovieId(movieId);
        updateMovie.setVoteCount(voteCount);
        updateMovie.setTotalScore(totalScore);
        updateMovie.setHotScore(calculateHotScore(updateMovie));
        sysMovieMapper.updateMovie(updateMovie);
    }

    /**
     * 根据电影的投票数量来动态地调整基础分在最终得分中所占的权重。
     * 当投票数量较少时，基础分权重更大，让电影得分更接近基础分，随着投票数增多，
     * 基础分权重逐渐降低，用户投票分数的权重逐渐增加，更能体现实际观众评价。
     * @return
     */
    public Double calculateHotScore(SysMovie movie) {
        double alpha = 50;
        double overallAverage = 6.0;
        // 根据投票数量计算基础分权重，这里简单用一个线性函数举例，可根据实际调整更合理的函数关系
        double baseScoreWeight = Math.max(0.5, 1 - (double) (movie.getVoteCount() + 1) / 100);
        double userScoreWeight = 1 - baseScoreWeight;
        double weightedBaseScore = 5.0 * baseScoreWeight;
        double weightedUserScore = (movie.getTotalScore() / movie.getVoteCount()) * userScoreWeight;
        return weightedBaseScore + weightedUserScore;
    }

}
